package com.springbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class SubmitBikeController extends SimpleFormController {

	private RentABike facade;
	
	/*public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String manufacturer=arg0.getParameter("manufacturer");
		String model=arg0.getParameter("model");
		String serialNo=arg0.getParameter("serialNo");
		double weight=Double.parseDouble(arg0.getParameter("weight"));
		String status=arg0.getParameter("status");
		int frame= Integer.parseInt(arg0.getParameter("frame"));
		
		facade.saveBike(new Bike(manufacturer, model, frame, serialNo, weight, status));
		// TODO Auto-generated method stub
		return new ModelAndView("bikes.jsp", "rentaBike", facade);
	}*/
	
	public ModelAndView onSubmit(Object command) {
		Bike bike = (Bike) command;
		facade.saveBike(bike);
		return new ModelAndView(new RedirectView(getSuccessView()), "rentaBike", facade);
	}
	
	protected Object formBackingObject(HttpServletRequest req) throws Exception {
		Bike bike = new Bike();
		if (req.getParameter("serialNo")!=null) {
			String serialNo=req.getParameter("serialNo");
			bike=facade.getBike(serialNo);
		}
		return bike;
	}

	public RentABike getFacade() {
		return facade;
	}

	public void setFacade(RentABike facade) {
		this.facade = facade;
	}

}
